import { anyGet, anyPost } from '@/network/anyRequest'

// 渠道信息相关接口
export interface ChannelInfo {
  channelId: string
  channelName: string
  available: boolean
  supportedPayMethods: string[]
  defaultImplementation: string
  implementations: string[]
  implementationCount: number
}

// 商户信息相关接口
export interface MerchantInfo {
  merchantId: string
  merchantName: string
  merchantType: 'NORMAL' | 'SUB_MERCHANT' // 普通商户 | 特约商户
  status: 'ACTIVE' | 'INACTIVE' | 'PENDING' // 激活 | 停用 | 审核中
  merchantNo: string
  contactInfo: {
    name: string
    phone: string
    email: string
  }
  businessInfo: {
    businessName: string
    businessType: string
    businessLicense: string
  }
}

// 服务商信息相关接口
export interface ServiceProviderInfo {
  serviceProviderId: string
  serviceProviderName: string
  serviceProviderNo: string
  supportedChannels: string[]
  subMerchants: string[]
  status: 'ACTIVE' | 'INACTIVE'
}

// 商户渠道配置相关接口
export interface MerchantChannelConfig {
  merchantId: string
  channelId: string
  configStatus: 'CONFIGURED' | 'NOT_CONFIGURED' // 已配置 | 未配置
  certificationStatus: 'CERTIFIED' | 'PENDING' | 'REJECTED' // 已认证 | 审核中 | 被拒绝
  feeRate: number
  configData: Record<string, any>
}

export interface ChannelImplementationInfo {
  implementationKey: string
  channelId: string
  implementationType: string
  channelName: string
  available: boolean
  supportedPayProducts: string[]
  default: boolean
}

// 支付请求相关接口
export interface PaymentRequest {
  merchantOrderId: string
  channelId: string
  implementationType: string
  payProduct: string
  amount: string
  currency: string
  subject: string
  body: string
  notifyUrl: string
  returnUrl: string
}

export interface PaymentResult {
  orderId: string
  merchantOrderId: string
  channelId: string
  amount: string
  currency: string
  status: string
  payUrl?: string
  qrCode?: string
  message?: string
  createTime?: string
  updateTime?: string
}

// 支付测试结果
export interface PaymentTestResult {
  success: boolean
  orderId: string
  channelId: string
  implType: string
  payProduct: string
  amount: string
  paymentResult: PaymentResult
  message?: string
}

// API 接口
export class PaymentApi {

  /**
   * 获取所有可用渠道
   */
  static async getAvailableChannels(): Promise<string[]> {
    const response = await anyGet('/api/payment/test/channels')
    return response.data || response
  }

  /**
   * 获取渠道信息
   */
  static async getChannelInfo(channelId: string): Promise<ChannelInfo> {
    const response = await anyGet(`/api/payment/test/channels/${channelId}`)
    return response.data || response
  }

  /**
   * 获取所有渠道的增强信息
   */
  static async getAllEnhancedChannelInfo(): Promise<ChannelInfo[]> {
    const response = await anyGet('/api/payment/test/channels/enhanced')
    return response.data || response
  }

  /**
   * 获取所有渠道实现信息（按注册顺序）
   */
  static async getAllChannelImplementationInfo(): Promise<ChannelImplementationInfo[]> {
    const response = await anyGet('/api/payment/test/channels/implementations/ordered')
    return response.data || response
  }

  /**
   * 创建支付订单
   */
  static async createPayment(request: PaymentRequest): Promise<PaymentResult> {
    const response = await anyPost('/api/payment/test/create', request)
    return response.data || response
  }

  /**
   * 模拟支付测试
   */
  static async simulatePayments(): Promise<any> {
    const response = await anyPost('/api/payment/test/simulate')
    return response.data || response
  }

  /**
   * 指定渠道支付测试
   */
  static async simulateSpecificPayment(
    channelId: string,
    implType: string,
    payProduct: string,
    amount: string = '100.00'
  ): Promise<PaymentTestResult> {
    const response = await anyPost(`/api/payment/test/simulate/${channelId}/${implType}/${payProduct}?amount=${amount}`)
    return response.data || response
  }

  /**
   * 获取支付产品列表
   */
  static async getPayProducts(): Promise<string[]> {
    const response = await anyGet('/api/payment/test/products')
    return response.data || response
  }

  /**
   * 获取框架信息
   */
  static async getFrameworkInfo(): Promise<any> {
    const response = await anyGet('/api/payment/test/info')
    return response.data || response
  }

  /**
   * 获取商户信息列表
   */
  static async getMerchantList(): Promise<MerchantInfo[]> {
    // TODO: 实现真实的API调用
    // const response = await anyGet('/api/payment/test/merchants')
    // return response.data || response

    // 模拟数据
    return [
      {
        merchantId: 'MCH001',
        merchantName: '测试普通商户',
        merchantType: 'NORMAL',
        status: 'ACTIVE',
        merchantNo: 'NORMAL_MCH_001',
        contactInfo: {
          name: '张三',
          phone: '13800138000',
          email: 'zhangsan@example.com'
        },
        businessInfo: {
          businessName: '测试商户有限公司',
          businessType: '电子商务',
          businessLicense: '91110000000000000X'
        }
      },
      {
        merchantId: 'MCH002',
        merchantName: '测试特约商户',
        merchantType: 'SUB_MERCHANT',
        status: 'ACTIVE',
        merchantNo: 'SUB_MCH_002',
        contactInfo: {
          name: '李四',
          phone: '13800138001',
          email: 'lisi@example.com'
        },
        businessInfo: {
          businessName: '特约商户有限公司',
          businessType: '零售',
          businessLicense: '91110000000000001X'
        }
      },
      {
        merchantId: 'MCH003',
        merchantName: '测试普通商户2',
        merchantType: 'NORMAL',
        status: 'PENDING',
        merchantNo: 'NORMAL_MCH_003',
        contactInfo: {
          name: '王五',
          phone: '13800138002',
          email: 'wangwu@example.com'
        },
        businessInfo: {
          businessName: '待审核商户有限公司',
          businessType: '服务业',
          businessLicense: '91110000000000002X'
        }
      }
    ]
  }

  /**
   * 获取服务商信息
   */
  static async getServiceProviderInfo(merchantId: string): Promise<ServiceProviderInfo | null> {
    // TODO: 实现真实的API调用
    // const response = await anyGet(`/api/payment/test/merchants/${merchantId}/service-provider`)
    // return response.data || response

    // 模拟数据 - 仅特约商户返回服务商信息
    if (merchantId === 'MCH002') {
      return {
        serviceProviderId: 'SP001',
        serviceProviderName: 'XX支付服务商',
        serviceProviderNo: 'SP_001',
        supportedChannels: ['wechat', 'alipay'],
        subMerchants: ['MCH002'],
        status: 'ACTIVE'
      }
    }
    return null
  }

  /**
   * 获取商户渠道配置
   */
  static async getMerchantChannelConfig(merchantId: string): Promise<MerchantChannelConfig[]> {
    // TODO: 实现真实的API调用
    // const response = await anyGet(`/api/payment/test/merchants/${merchantId}/channel-config`)
    // return response.data || response

    // 模拟数据
    if (merchantId === 'MCH001') {
      return [
        {
          merchantId: 'MCH001',
          channelId: 'channeltest1',
          configStatus: 'CONFIGURED',
          certificationStatus: 'CERTIFIED',
          feeRate: 0.6,
          configData: {
            appId: 'wx1234567890',
            appSecret: 'secret123'
          }
        },
        {
          merchantId: 'MCH001',
          channelId: 'channeltest2',
          configStatus: 'NOT_CONFIGURED',
          certificationStatus: 'PENDING',
          feeRate: 0.6,
          configData: {}
        }
      ]
    }
    return []
  }
}

// 导出类型和API
export default PaymentApi